package cn.wildfire.chat.app.main;

import android.content.Intent;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.google.gson.JsonObject;
import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;
import com.sourcemuch.im.caiba.R2;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import cn.wildfire.chat.app.extendsion.classes.ResponseEntity;
import cn.wildfire.chat.app.extendsion.classes.ResponsePhoneNumberUserInfo;
import cn.wildfire.chat.app.extendsion.https.OkHttpRequest;
import cn.wildfire.chat.app.extendsion.https.ReqCallBack;
import cn.wildfire.chat.app.extendsion.https.ServerUrlClient;
import cn.wildfire.chat.app.extendsion.ui.ToastTip;
import cn.wildfire.chat.app.extendsion.utils.TextUtils;
import cn.wildfire.chat.kit.WfcBaseActivity;

public class SetNewPasswordActivity extends WfcBaseActivity {

    @BindView(R2.id.et_new_password)
    EditText etNewPassword;
    @BindView(R2.id.et_new_password_cheack)
    EditText etNewPasswordCheack;

    ResponsePhoneNumberUserInfo responsePhoneNumberUserInfo;
    @BindView(R2.id.iv_new_password_hide_or_show)
    ImageView ivNewPasswordHideOrShow;
    @BindView(R2.id.iv_new_password_check_hide_or_show)
    ImageView ivNewPasswordCheckHideOrShow;


    @Override
    protected void afterViews() {
        super.afterViews();
        Intent intent = getIntent();
        responsePhoneNumberUserInfo = intent.getParcelableExtra("UserInfo");
        etNewPassword.setHint(getString(R.string.et_input_new_password_hint) + "" + getString(R.string.password_hint_specification));
        etNewPasswordCheack.setHint(getString(R.string.et_input_new_password_cheack_hint) + "" + getString(R.string.password_hint_specification));
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_set_new_password;
    }

    @Override
    protected void setTitleCenterThenSetSearchView() {
        super.setTitleCenterThenSetSearchView();
        setToolbarOrReplaceType(true, 2);
        setReplaceTitle(null, R.string.set_new_password);
        setSearchLayoutVisibility(false);
    }

    private boolean newPasswordIsShow;
    private boolean newPasswordCheckIsShow;

    @OnClick({R2.id.btn_set_security_question_finish, R2.id.iv_new_password_hide_or_show, R2.id.iv_new_password_check_hide_or_show})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btn_set_security_question_finish) {
            setNewPassword();
        } else if (id == R.id.iv_new_password_hide_or_show) {
            if (newPasswordIsShow) {
                newPasswordIsShow = false;
                ivNewPasswordHideOrShow.setImageResource(R.mipmap.password_hide);
                etNewPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                etNewPassword.setSelection(etNewPassword.getText().toString().length());
            } else {
                newPasswordIsShow = true;
                ivNewPasswordHideOrShow.setImageResource(R.mipmap.password_show);
                etNewPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD + 1);
                etNewPassword.setSelection(etNewPassword.getText().toString().length());
            }
        } else if (id == R.id.iv_new_password_check_hide_or_show) {
            if (newPasswordCheckIsShow) {
                newPasswordCheckIsShow = false;
                ivNewPasswordCheckHideOrShow.setImageResource(R.mipmap.password_hide);
                etNewPasswordCheack.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                etNewPasswordCheack.setSelection(etNewPasswordCheack.getText().toString().length());
            } else {
                newPasswordCheckIsShow = true;
                ivNewPasswordCheckHideOrShow.setImageResource(R.mipmap.password_show);
                etNewPasswordCheack.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD + 1);
                etNewPasswordCheack.setSelection(etNewPasswordCheack.getText().toString().length());
            }
        }
    }

    private void setNewPassword() {

        String newPassword = etNewPassword.getText().toString().trim();
        String newPasswordCheck = etNewPasswordCheack.getText().toString().trim();

        if (TextUtils.isTextEmpty(newPassword) || newPassword.length() < 6) {
            ToastTip.tip(getString(R.string.checked_password_is_less));
            return;
        }

        if (TextUtils.isTextEmpty(newPasswordCheck) || newPasswordCheck.length() < 6) {
            ToastTip.tip(getString(R.string.checked_password_is_less));
            return;
        }

        if (!newPassword.equals(newPasswordCheck)) {
            ToastTip.tip(getString(R.string.checked_password_not_same));
            return;
        }

        if (newPassword.length() > 12 || newPasswordCheck.length() > 12) {
            ToastTip.tip(getString(R.string.checked_password_is_too_long));
            return;
        }

        HashMap<String, Object> params = new HashMap<>();
        params.put("confirmPassword", newPasswordCheck);
        params.put("password", newPassword);
        params.put("uid", responsePhoneNumberUserInfo.getResult().getUid());

        OkHttpRequest.getInstance(this).requestAsynJsonObject(ServerUrlClient.RESET_PASSWORD, params, new ReqCallBack<Object>() {
            @Override
            public void onReqSuccess(Object result) {
                ResponseEntity<JsonObject, String> responseEntity = new ResponseEntity<>(result.toString());
                if (responseEntity.getErrorCode().equals("0")) {
                    ToastTip.tip(getString(R.string.reset_success));
                    Intent intent = new Intent(SetNewPasswordActivity.this, LoginActivity.class);
//                    intent.putExtra("username",responsePhoneNumberUserInfo.getResult().getMobile());
                    startActivity(intent);
                    finish();
                } else {
                    ToastTip.tip(responseEntity.message);
                }

            }

            @Override
            public void onReqFailed(String errorMsg) {
                ToastTip.tip(getString(R.string.request_failure));
            }
        }).request();
    }
}

